//Created by Kyle "Fayt" Lowry
//EndGame.cpp

#include "EndGame.h"
#include "input.h"
#include "Controller.h"
#include <stdio.h>

void EndGame::Init(Sprite* a_sprite, Input* a_input, int a_winner)
{
	sprite			= a_sprite;
	EndGameInput	= a_input;
	winner			= a_winner;

	//Initialize the buttons
	for(int i = 0; i < NUM_END_GAME_BUTTONS; i++)
	{
		//Load the button texture for the buttons
		button[i].loadTexture("button.png");

		//Set the current button's type
		button[i].setButtonType(i);
	}

	//Set the button's positions appropriately
	button[0].setPos(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2);						//Return to menu button
	button[1].setPos(SCREEN_WIDTH - (SCREEN_WIDTH / 4), SCREEN_HEIGHT / 2);		//Replay game button
	button[2].setPos(SCREEN_WIDTH / 2, SCREEN_HEIGHT - (SCREEN_HEIGHT / 6) );	//End game button

	//Initialize the selected button
	selectedButton.loadTexture("buttonSelected.png");
	selectedButton.setPos( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
	selectedButton.setButtonType(RETURN_BUTTON);

	//Initialize the end game sprite (which will be used for the background)
	EndGameSprite[0].loadTexture("Gold_backdrop.png");
	EndGameSprite[0].setScale(1.6f, 1.2f);
	EndGameSprite[0].setPosOfCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

	//Initialize the end game sprite (which will be the winning player's character image)
	EndGameSprite[1].loadTexture(Controller::Instance()->getWinningPlayerPic() );
	EndGameSprite[1].setScale(0.6f, 0.6f);
	EndGameSprite[1].setPosOfCenter(SCREEN_WIDTH / 2, SCREEN_HEIGHT - ( (SCREEN_HEIGHT / 5) + (SCREEN_HEIGHT / 5) ) );

	//Initialize the text objects
	text[0] = new Text(SCREEN_HEIGHT / 9, false, false, "Jokerman");
	text[0]->setPosAndSize(SCREEN_WIDTH / 8, SCREEN_HEIGHT / 6, SCREEN_WIDTH - (SCREEN_WIDTH / 8), SCREEN_HEIGHT / 2);
	text[0]->setText(" ");

	text[1] = new Text(SCREEN_HEIGHT / 16, false, false, "Jokerman");
	text[1]->setText(" ");
}

CONTROLLER_STATE EndGame::Update()
{
	//Mouse input for the different buttons the player can click at the end game screen
	for(int i = 0; i < NUM_END_GAME_BUTTONS; i++)
	{
		//If the mouse is over the current button
		if(button[i].inButton(EndGameInput->getMousePosition()))
		{
			//If so check what the type of that button is in order to send the controller the proper state
			if(EndGameInput->leftClicked())
			{
				switch(button[i].getButtonType())
				{
				case MENU_BUTTON:
					{
						return MENU;

						break;
					}
				case REPLAY_BUTTON:
					{
						return GAME;

						break;
					}
				case END_GAME_BUTTON:
					{
						PostQuitMessage(0);

						break;
					}
				}
			}
		}
	}

	for(int i = 0; i < NUM_PLAYERS; i++)
	{
		if(i == winner)
		{
			//Create the winning player's text and put it in buffer
			sprintf_s(buffer, 64, "Congratulations Player %i,\nYou Win!", winner + 1);
			
			//Set the winning player's text for the end game screen
			text[0]->setColor(255,0,0,255);
			text[0]->setText(buffer);
		}
	}

	return END_GAME;
}

void EndGame::Render()
{
	for(int i = 0; i < NUM_END_GAME_SPRITES; i++)
	{
			sprite->Draw(EndGameSprite[i]);
	}

	for(int i = 0; i < NUM_END_GAME_BUTTONS; ++i)
	{
		//If the mouse is over the current button
		if(button[i].inButton(EndGameInput->getMousePosition()))
		{
			//Set the selected button sprite accordingly
			selectedButton.setPos( button[i].getSpriteInfo().getPosOfCenter().X, button[i].getSpriteInfo().getPosOfCenter().Y);
			selectedButton.setButtonType( button[i].getButtonType() );

			//Draw the selected button
			sprite->Draw( selectedButton.getSpriteInfo() );
		}
		else
		{
			//Else draw the button with its texture regularly
			sprite->Draw(button[i].getSpriteInfo());
		}
	}

	//Render the text that was set in update
	text[0]->renderCentered();

	//Button 0 text
	//Set and render the button text
	text[1]->setPosAndSize( button[0].getSpriteInfo().getPosOfCenter().X - button[0].getSpriteInfo().getCenter().X,		//Left side of the rect
							button[0].getSpriteInfo().getPosOfCenter().Y - button[0].getSpriteInfo().getCenter().Y,		//Top of the rect
							button[0].getSpriteInfo().getPosOfCenter().X + button[0].getSpriteInfo().getCenter().X,		//Right side of the rect
							button[0].getSpriteInfo().getPosOfCenter().Y + button[0].getSpriteInfo().getCenter().Y );	//Bottom of the rect
	//Create the button text and put it in a buffer
	sprintf_s(buffer, 64, "MENU");
	//Set the winning player's text for the end game screen
	text[1]->setColor(0,0,255,255);
	text[1]->setText(buffer);
	//Render the text
	text[1]->renderCentered();

	//Button 1 text
	//Set and render the button text
	text[1]->setPosAndSize( button[1].getSpriteInfo().getPosOfCenter().X - button[1].getSpriteInfo().getCenter().X,		//Left side of the rect
							button[1].getSpriteInfo().getPosOfCenter().Y - button[1].getSpriteInfo().getCenter().Y,		//Top of the rect
							button[1].getSpriteInfo().getPosOfCenter().X + button[1].getSpriteInfo().getCenter().X,		//Right side of the rect
							button[1].getSpriteInfo().getPosOfCenter().Y + button[1].getSpriteInfo().getCenter().Y );	//Bottom of the rect
	//Create the button text and put it in a buffer
	sprintf_s(buffer, 64, "REPLAY");
	text[1]->setText(buffer);
	//Render the text
	text[1]->renderCentered();

	//Button 2 text
	//Set and render the button text
	text[1]->setPosAndSize( button[2].getSpriteInfo().getPosOfCenter().X - button[2].getSpriteInfo().getCenter().X,		//Left side of the rect
							button[2].getSpriteInfo().getPosOfCenter().Y - button[2].getSpriteInfo().getCenter().Y,		//Top of the rect
							button[2].getSpriteInfo().getPosOfCenter().X + button[2].getSpriteInfo().getCenter().X,		//Right side of the rect
							button[2].getSpriteInfo().getPosOfCenter().Y + button[2].getSpriteInfo().getCenter().Y );	//Bottom of the rect
	//Create the button text and put it in a buffer
	sprintf_s(buffer, 64, "QUIT");
	text[1]->setText(buffer);
	//Render the text
	text[1]->renderCentered();
}

void EndGame::Release()
{
	//Release the end game's sprite object
	//sprite->Release();

	//Release the end game buttons
}